﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Caifu.Clients;
using Itfort.Web;

namespace Caifu.WebAdmin.NetValues
{
	/// <summary>
	/// 列出所有基金，并且跟上其净值。
	/// 最近一个交易日（当天为交易日时，18点钟之前，显示前一交易日）。
	/// </summary>
	public partial class TodayNetValue : System.Web.UI.Page
	{
		protected int m_i;
		protected int type;
		protected bool qdii;

		protected void Page_Load(object sender, EventArgs e)
		{
			type = RequestUtil.ToInt32("type");
			qdii = RequestUtil.ToBoolean("qdii");

			var date = TradeDateUtil.GetNetDailyDate();
			var funds = FundClient.GetAllFunds();
			var netValues = NetValueClient.GetNetValues(date);
			var netValueDict = netValues.ToDictionary(p => p.FundId, p => p);

			DataTable dataSource = new DataTable();
			dataSource.Columns.Add("FundCode");
			dataSource.Columns.Add("FundName");
			dataSource.Columns.Add("Established", typeof(DateTime));
			dataSource.Columns.Add("UnitNetValue");
			dataSource.Columns.Add("AccumulatedNetValue");

			foreach (var fund in funds)
			{
				if (fund.FundType != type) continue;

				var row = dataSource.NewRow();
				dataSource.Rows.Add(row);
				row["FundCode"] = fund.Code;
				row["FundName"] = fund.Name;
				row["Established"] = fund.Established;

				NetValue netValue;
				var exist = netValueDict.TryGetValue(fund.Id, out netValue);
				if (exist)
				{
					row["UnitNetValue"] = netValue.UnitNetValue;
					row["AccumulatedNetValue"] = netValue.AccumulatedNetValue;
				}
			}

			this.Repeater1.DataSource = dataSource;
			this.DataBind();
		}
	}
}